<?php

namespace Psg;

import('Psg.Core');

class Messages {

    /**
     * Zwraca połączenie z bazą danych
     * 
     * @return \Db\Connection Obiekt połączenia
     */
    private static function dbHandler() {
        $Db = \Db\Connection::Open();
        return $Db;
    }

    public static function countUnReadedMsg($email) {
        $uid = Core::getUserIdByMail($email);
        $Where = \Db\Operators::Create()
                        ->C('to')->O('=')->V($uid)->O('and')
                        ->C('unread')->O('=')->V('1');
        $Q = \Db\Query::Create('inbox')
                ->Select()
                ->Where($Where);
        return count(self::dbHandler()->Query($Q));
    }

    public static function formatMessage($msg) {
        $msg = preg_replace('/\*(.*?)/', "&emsp;&bull;&emsp;$1", $msg);
        $msg = preg_replace('/\r\n|\n|\r/', '<br/>', $msg);
        return $msg;
    }

    public static function getMessagesToUser($uid) {
        $Where = \Db\Operators::Create()
                        ->C('to')->O('=')->V($uid);
        $Q = \Db\Query::Create('inbox')
                ->Select()
                ->Where($Where)
                ->OrderBy(\Db\Operators::Create()->C('time')->O('desc'));
        return self::dbHandler()->Query($Q);
    }

    public static function getMessage($id, $mailbox = 'inbox') {
        $Where = \Db\Operators::Create()
                        ->C('id')->O('=')->V($id);
        $Q = \Db\Query::Create($mailbox)
                ->Select()
                ->Where($Where);
        $res = self::dbHandler()->Query($Q);
        if (count($res) == 1) {
            if ($mailbox == 'inbox') {
                $Q1 = \Db\Query::Create('inbox')->Update()
                        ->Set(\Db\Operators::Create()
                                ->C('unread')->O('=')->V(0))
                        ->Where(\Db\Operators::Create()->C('id')->O('=')->V($id));
                self::dbHandler()->Query($Q1);
            }
            return $res[0];
        } else {
            return 'PSG:MSGNOTEXISTS';
        }
    }

    public static function delMessage($id, $mailbox = 'inbox') {
        $Where = \Db\Operators::Create()
                        ->C('id')->O('=')->V($id);
        $Q = \Db\Query::Create($mailbox)
                ->Delete()
                ->Where($Where);
        self::dbHandler()->Query($Q);
    }

    public static function writeMessage($from, $to, $topic, $message) {
        $Dbh = self::dbHandler();
        $topic = htmlspecialchars($topic);
        $message = htmlspecialchars($message);
        $Oper1 = \Db\Operators::Create()
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?');
        $Query1 = \Db\Query::Create('inbox')
                ->Insert($Oper1, ['from', 'to', 'topic', 'message', 'unread', 'time']);
        $timestamp = date('Y-m-d H:i:s');
        $Dbh->Prepare($Query1, [$from, $to, $topic, $message, 1, $timestamp]);
        $Oper2 = \Db\Operators::Create()
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?')->O(',')
                ->O('?');
        $Query2 = \Db\Query::Create('outbox')
                ->Insert($Oper2, ['from', 'to', 'topic', 'message', 'time']);
        $Dbh->Prepare($Query2, [$from, $to, $topic, $message, $timestamp]);
    }

}